//
// spdiags.cpp
//
// Code generation for function 'spdiags'
//

// Include files
#include "spdiags.h"
#include "detrendIBI_priors_data.h"
#include "rt_nonfinite.h"
#include "sparse.h"
#include "sparse1.h"
#include "coder_array.h"
#include "mwmathutil.h"

// Variable Definitions
static emlrtRSInfo ic_emlrtRSI{
    173,       // lineNo
    "spdiags", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pathName
};

static emlrtRSInfo jc_emlrtRSI{
    136,       // lineNo
    "spdiags", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pathName
};

static emlrtRSInfo kc_emlrtRSI{
    135,       // lineNo
    "spdiags", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pathName
};

static emlrtRSInfo
    lc_emlrtRSI{
        28,      // lineNo
        "colon", // fcnName
        "D:\\Program "
        "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\ops\\colon.m" // pathName
    };

static emlrtRTEInfo k_emlrtRTEI{
    71,        // lineNo
    13,        // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

static emlrtRTEInfo l_emlrtRTEI{
    80,        // lineNo
    13,        // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

static emlrtRTEInfo m_emlrtRTEI{
    116,       // lineNo
    9,         // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

static emlrtDCInfo c_emlrtDCI{
    124,       // lineNo
    5,         // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m", // pName
    4 // checkKind
};

static emlrtRTEInfo ic_emlrtRTEI{
    124,       // lineNo
    5,         // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

static emlrtRTEInfo jc_emlrtRTEI{
    125,       // lineNo
    5,         // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

static emlrtRTEInfo kc_emlrtRTEI{
    129,       // lineNo
    9,         // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

static emlrtRTEInfo
    lc_emlrtRTEI{
        28,      // lineNo
        9,       // colNo
        "colon", // fName
        "D:\\Program "
        "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\ops\\colon.m" // pName
    };

static emlrtRTEInfo mc_emlrtRTEI{
    135,       // lineNo
    9,         // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

static emlrtRTEInfo nc_emlrtRTEI{
    136,       // lineNo
    9,         // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

static emlrtRTEInfo oc_emlrtRTEI{
    137,       // lineNo
    15,        // colNo
    "spdiags", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\spdiags.m" // pName
};

// Function Definitions
namespace coder {
void spdiags(const emlrtStack *sp, const ::coder::array<real_T, 2U> &arg1,
             real_T arg3, real_T arg4, sparse *res1)
{
  array<real_T, 2U> idx;
  array<real_T, 1U> aDat;
  array<real_T, 1U> i;
  array<int32_T, 2U> r;
  array<int32_T, 1U> aCols;
  array<int32_T, 1U> aRows;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack st;
  real_T len_data[4];
  real_T minAdjustedDim_data[3];
  real_T d;
  real_T maxIndex;
  int32_T k;
  int32_T mGEn;
  int32_T partialTrueCount;
  int32_T trueCount;
  int8_T d_data[3];
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b((emlrtCTX)sp);
  if ((!(arg3 == muDoubleScalarFloor(arg3))) || (!(arg3 >= 0.0))) {
    emlrtErrorWithMessageIdR2018a(sp, &k_emlrtRTEI,
                                  "Coder:toolbox:spdiagsBadSize",
                                  "Coder:toolbox:spdiagsBadSize", 3, 4, 1, "m");
  }
  if (!(arg4 == muDoubleScalarFloor(arg4))) {
    emlrtErrorWithMessageIdR2018a(sp, &l_emlrtRTEI,
                                  "Coder:toolbox:spdiagsBadSize",
                                  "Coder:toolbox:spdiagsBadSize", 3, 4, 1, "n");
  }
  trueCount = 0;
  if ((-arg3 + 1.0 <= 0.0) && (arg4 - 1.0 >= 0.0)) {
    trueCount = 1;
  }
  if (arg4 - 1.0 >= 1.0) {
    trueCount++;
  }
  if (arg4 - 1.0 >= 2.0) {
    trueCount++;
  }
  partialTrueCount = 0;
  if ((-arg3 + 1.0 <= 0.0) && (arg4 - 1.0 >= 0.0)) {
    d_data[0] = 0;
    partialTrueCount = 1;
  }
  if (arg4 - 1.0 >= 1.0) {
    d_data[partialTrueCount] = 1;
    partialTrueCount++;
  }
  if (arg4 - 1.0 >= 2.0) {
    d_data[partialTrueCount] = 2;
  }
  mGEn = (arg3 >= arg4);
  len_data[0] = 0.0;
  for (partialTrueCount = 0; partialTrueCount < trueCount; partialTrueCount++) {
    k = d_data[partialTrueCount];
    d = muDoubleScalarMin(arg3, arg4 - static_cast<real_T>(k));
    minAdjustedDim_data[partialTrueCount] = d;
    if (d < 1.0) {
      maxIndex = 0.0;
    } else {
      maxIndex = d;
      if (arg3 >= arg4) {
        maxIndex = d + static_cast<real_T>(k);
      }
    }
    if (maxIndex > arg1.size(0)) {
      emlrtErrorWithMessageIdR2018a(sp, &m_emlrtRTEI,
                                    "MATLAB:spdiags:InvalidSizeBFourInput",
                                    "MATLAB:spdiags:InvalidSizeBFourInput", 0);
    }
    len_data[partialTrueCount + 1] =
        ((len_data[partialTrueCount] + d) - 1.0) + 1.0;
  }
  if (!(len_data[trueCount] >= 0.0)) {
    emlrtNonNegativeCheckR2012b(len_data[trueCount], &c_emlrtDCI, (emlrtCTX)sp);
  }
  partialTrueCount = static_cast<int32_T>(len_data[trueCount]);
  aRows.set_size(&ic_emlrtRTEI, sp, partialTrueCount);
  if (!(len_data[trueCount] >= 0.0)) {
    emlrtNonNegativeCheckR2012b(len_data[trueCount], &c_emlrtDCI, (emlrtCTX)sp);
  }
  for (k = 0; k < partialTrueCount; k++) {
    aRows[k] = 0;
  }
  aCols.set_size(&jc_emlrtRTEI, sp, partialTrueCount);
  for (k = 0; k < partialTrueCount; k++) {
    aCols[k] = 0;
  }
  aDat.set_size(&kc_emlrtRTEI, sp, partialTrueCount);
  for (k = 0; k < partialTrueCount; k++) {
    aDat[k] = 0.0;
  }
  for (int32_T b_k{0}; b_k < trueCount; b_k++) {
    int32_T nm1d2;
    boolean_T guard1{false};
    st.site = &kc_emlrtRSI;
    b_st.site = &lc_emlrtRSI;
    d = minAdjustedDim_data[b_k];
    if (d < 1.0) {
      idx.set_size(&lc_emlrtRTEI, &b_st, 1, 0);
    } else if (muDoubleScalarIsInf(d) && (d == 1.0)) {
      idx.set_size(&lc_emlrtRTEI, &b_st, 1, 1);
      idx[0] = rtNaN;
    } else {
      partialTrueCount = static_cast<int32_T>(d - 1.0);
      idx.set_size(&lc_emlrtRTEI, &b_st, 1, static_cast<int32_T>(d - 1.0) + 1);
      for (k = 0; k <= partialTrueCount; k++) {
        idx[k] = static_cast<real_T>(k) + 1.0;
      }
    }
    i.set_size(&mc_emlrtRTEI, sp, idx.size(1));
    partialTrueCount = idx.size(1);
    for (k = 0; k < partialTrueCount; k++) {
      i[k] = idx[k];
    }
    st.site = &jc_emlrtRSI;
    b_st.site = &lc_emlrtRSI;
    d = len_data[b_k];
    guard1 = false;
    if (muDoubleScalarIsNaN(d + 1.0)) {
      guard1 = true;
    } else {
      maxIndex = len_data[b_k + 1];
      if (muDoubleScalarIsNaN(maxIndex)) {
        guard1 = true;
      } else if (maxIndex < d + 1.0) {
        idx.set_size(&nc_emlrtRTEI, &b_st, 1, 0);
      } else if ((muDoubleScalarIsInf(d + 1.0) ||
                  muDoubleScalarIsInf(maxIndex)) &&
                 (d + 1.0 == maxIndex)) {
        idx.set_size(&nc_emlrtRTEI, &b_st, 1, 1);
        idx[0] = rtNaN;
      } else if (muDoubleScalarFloor(d + 1.0) == d + 1.0) {
        partialTrueCount = static_cast<int32_T>(maxIndex - (d + 1.0));
        idx.set_size(&nc_emlrtRTEI, &b_st, 1, partialTrueCount + 1);
        for (k = 0; k <= partialTrueCount; k++) {
          idx[k] = (len_data[b_k] + 1.0) + static_cast<real_T>(k);
        }
      } else {
        real_T apnd;
        real_T cdiff;
        real_T ndbl;
        c_st.site = &ab_emlrtRSI;
        ndbl = muDoubleScalarFloor((maxIndex - (d + 1.0)) + 0.5);
        apnd = (d + 1.0) + ndbl;
        cdiff = apnd - maxIndex;
        if (muDoubleScalarAbs(cdiff) <
            4.4408920985006262E-16 *
                muDoubleScalarMax(muDoubleScalarAbs(d + 1.0),
                                  muDoubleScalarAbs(maxIndex))) {
          ndbl++;
          apnd = maxIndex;
        } else if (cdiff > 0.0) {
          apnd = (d + 1.0) + (ndbl - 1.0);
        } else {
          ndbl++;
        }
        if (ndbl >= 0.0) {
          partialTrueCount = static_cast<int32_T>(ndbl);
        } else {
          partialTrueCount = 0;
        }
        d_st.site = &bb_emlrtRSI;
        if (ndbl > 2.147483647E+9) {
          emlrtErrorWithMessageIdR2018a(&d_st, &emlrtRTEI,
                                        "Coder:MATLAB:pmaxsize",
                                        "Coder:MATLAB:pmaxsize", 0);
        }
        idx.set_size(&sb_emlrtRTEI, &c_st, 1, partialTrueCount);
        if (partialTrueCount > 0) {
          idx[0] = d + 1.0;
          if (partialTrueCount > 1) {
            idx[partialTrueCount - 1] = apnd;
            nm1d2 = (partialTrueCount - 1) / 2;
            for (k = 0; k <= nm1d2 - 2; k++) {
              idx[k + 1] =
                  (len_data[b_k] + 1.0) + (static_cast<real_T>(k) + 1.0);
              idx[(partialTrueCount - k) - 2] =
                  apnd - (static_cast<real_T>(k) + 1.0);
            }
            if (nm1d2 << 1 == partialTrueCount - 1) {
              idx[nm1d2] = ((len_data[b_k] + 1.0) + apnd) / 2.0;
            } else {
              idx[nm1d2] = (len_data[b_k] + 1.0) + static_cast<real_T>(nm1d2);
              idx[nm1d2 + 1] = apnd - static_cast<real_T>(nm1d2);
            }
          }
        }
      }
    }
    if (guard1) {
      idx.set_size(&nc_emlrtRTEI, &b_st, 1, 1);
      idx[0] = rtNaN;
    }
    r.set_size(&oc_emlrtRTEI, sp, 1, idx.size(1));
    partialTrueCount = idx.size(1);
    for (k = 0; k < partialTrueCount; k++) {
      r[k] = static_cast<int32_T>(idx[k]);
    }
    partialTrueCount = r.size(1);
    for (k = 0; k < partialTrueCount; k++) {
      aRows[r[k] - 1] = static_cast<int32_T>(i[k]);
    }
    partialTrueCount = r.size(1);
    for (k = 0; k < partialTrueCount; k++) {
      aCols[r[k] - 1] =
          static_cast<int32_T>(i[k] + static_cast<real_T>(d_data[b_k]));
    }
    nm1d2 = mGEn * d_data[b_k];
    partialTrueCount = r.size(1);
    for (k = 0; k < partialTrueCount; k++) {
      aDat[r[k] - 1] =
          arg1[(static_cast<int32_T>(i[k] + static_cast<real_T>(nm1d2)) +
                arg1.size(0) * b_k) -
               1];
    }
  }
  st.site = &ic_emlrtRSI;
  c_sparse(&st, aRows, aCols, aDat, arg3, arg4, res1);
  emlrtHeapReferenceStackLeaveFcnR2012b((emlrtCTX)sp);
}

} // namespace coder

// End of code generation (spdiags.cpp)
